﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;
using System.Text.RegularExpressions;
using CCWin;
using System.IO;

namespace BS_2
{
    public partial class Form_NewPrj : CCWin.Skin_DevExpress
    {
        public Form_NewPrj()
        {
            InitializeComponent();
        }
        private void submit_Click_1(object sender, EventArgs e)
        {
            DialogResult dr = MessageBoxEx.Show("确认新建项目吗？", "提示", MessageBoxButtons.OKCancel);
            if (dr == DialogResult.OK)
            {
                string prjcode = "";
                string name = prjname.Text.Trim();
                string creator = prjcreator.Text.Trim();
                string source = prjsource.Text.Trim();
                string date = DateTime.Now.ToString("yyyy-MM-dd");

                int rows = Convert.ToInt16(dataGridView1.Rows.Count.ToString()) - 1;
                if (name == "" || creator == "" || source == "")
                {
                    MessageBoxEx.Show("数据填写不完整，请检查！", "输入提示");
                    return;
                }
                if(skinCheckBox1.Checked==false || skinCheckBox1.Checked == false||
                    skinCheckBox1.Checked == false||skinCheckBox1.Checked == false)
                {
                    MessageBoxEx.Show("请至少勾选一项数据集", "输入提示");
                    return;
                }

                if (rows == 0)
                {
                    MessageBoxEx.Show("请输入项目范围拐点坐标！", "输入提示");
                    return;
                }
                else if (rows < 3)
                {
                    MessageBoxEx.Show("至少输入3个拐点坐标！", "输入提示");
                    return;
                }
                else
                {
                    for (int i = 0; i < rows; i++)
                    {
                        if (dataGridView1.Rows[i].Cells[0].Value == null || dataGridView1.Rows[i].Cells[1].Value == null)
                        {
                            MessageBoxEx.Show("拐点坐标必须填写完整！", "输入提示");
                            return;
                        }
                    }
                }
                string sql = "select prjname from project where prjname='" + name + "'";
                //MessageBox.Show(DataAccess.Get_Row1_Col1_Value("select prjname from project where prjname ='cheng'"));
                string resultquery = DataAccess.Get_Row1_Col1_Value(sql);
                if (resultquery != "")
                {
                    MessageBoxEx.Show("项目名称已存在，请检查！", "输入提示");
                    return;
                }
                //录入项目信息
                string checkedtext = CheckBoxReturn(skinCheckBox1) +CheckBoxReturn(skinCheckBox2) +  CheckBoxReturn(skinCheckBox3) +  CheckBoxReturn(skinCheckBox4);
                string checkedtext1 = checkedtext.Substring(0, checkedtext.Length - 1);
                //MessageBox.Show(datachecked);
                sql = "insert into project(prjname,prjcreator,prjdate,prjsource,prjdataset) values('" + name + "','" + creator + "','" + date + "','" + source + "','" + checkedtext1 + "')";
                int result = DataAccess.Run_SQL(sql);
                
                if (result == 1)
                {
                    //录入项目坐标
                    //MessageBox.Show("dataset是ok的");
                    sql = "select prjcode from project where prjname='" + name + "'";
                    prjcode = DataAccess.Get_Row1_Col1_Value(sql);
                    for (int i = 0; i < rows; i++)
                    {
                        int j = i + 1;
                        string sql_domain = "(" + prjcode + "," + j + "," + dataGridView1.Rows[i].Cells[0].Value + "," + dataGridView1.Rows[i].Cells[1].Value + ")";
                        sql = "insert into project_domain values" + sql_domain;
                        //MessageBox.Show(DataAccess.Get_Row1_Col1_Value("select prjcode from project_domain where prjcode ='1'"));
                        int resultdomain = DataAccess.Run_SQL(sql);
                        Console.WriteLine(resultdomain);//得到的结果是0，说明上面是有问题的
                        if (resultdomain == 0)
                        {
                            sql = "delete from project where prjcode=" + prjcode;
                            DataAccess.Run_SQL(sql);
                            sql = "delete from project_domain where prjcode=" + prjcode;
                            DataAccess.Run_SQL(sql);
                            MessageBoxEx.Show("新建项目失败！", "提示");
                            return;
                        }
                    }
                    MessageBoxEx.Show("新建项目成功！", "提示");
                    ClearText();

                }
                else
                {
                    MessageBoxEx.Show("新建项目失败！", "提示");
                }
            }
        }
        private void dataGridView1_RowPostPaint_1(object sender, DataGridViewRowPostPaintEventArgs e)
        {

            SolidBrush b = new SolidBrush(this.dataGridView1.RowHeadersDefaultCellStyle.ForeColor);
            e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), this.dataGridView1.DefaultCellStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4);
        }

        private void dataGridView1_CellValidating_1(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (Regex.IsMatch(e.FormattedValue.ToString(), @"^[+-]?\d*[.]?\d*$") == false)
            {
                e.Cancel = true;
                MessageBoxEx.Show("拐点坐标必须是数值型");
                return;
            }
        }

        private void close_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }


        private void ClearText()
        {
            prjname.Text = "";
            prjcreator.Text = "";
            prjsource.Text = "";
            dataGridView1.Rows.Clear();
        }


        //勾选了checked则返回text，否则返回空字符串
         public string CheckBoxReturn(CCWin.SkinControl.SkinCheckBox box)
        {
            if(box.Checked == false)
            {
                return "";
            }
            else
            {
                return box.Text+",";
            }
        }

        
    }
}
